package com.houtai.service.wiki;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.Page;
import com.houtai.base.ResultVo;
import com.houtai.bean.*;
import com.houtai.bean.dto.wiki.*;
import com.houtai.bean.vo.wiki.*;
import com.houtai.bean.vo.wiki.collection.TdWikiCollectionInsertDto;
import com.houtai.bean.vo.wiki.praise.TdWikiPraiseInsertDto;
import com.houtai.bean.vo.wiki.search.TdWikiSearchList;
import com.houtai.dao.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WikiService {
    private static Logger logger = Logger.getLogger(WikiService.class);

    @Autowired
    private TdWikiDao tdWikiDao;
    
    @Autowired
    private TdWikiConDao tdWikiConDao;

    @Autowired
    private TdWikiSearchDao tdWikiSearchDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TdWikiCollectionDao tdWikiCollectionDao;

    @Autowired
    private TdWikiPraiseDao tdWikiPraiseDao;

    @Autowired
    private TsJzDictDao tsJzDictDao;
    /**
     * @Purpose:    搜索百科
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public TdWikiListVo wikiList(TdWikiListDto dto) {
        String logInfo =  ":wikiList:";
        logger.info("======" + logInfo + "begin======");
        TdWikiListVo vo = new TdWikiListVo();
        try {

            String orderBy = "wiki_id asc";
            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
            List<TdWikiList> lists = tdWikiDao.findTdWikiList(dto);
            PageInfo<TdWikiList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {
                vo.setCurrent_page(dto.getCurrent_page());
                vo.setList(list.getList());
                vo.setPage_size(list.getPageSize());
                vo.setTotal_record(list.getTotal());
                vo.setPages(list.getPages());
                vo.setMark("0");
                vo.setTip("查询成功");
            } else {
                vo.setMark("1");
                vo.setTip("无记录");
            }
            TdWikiSearch tdWikiSearch = tdWikiSearchDao.findTdWikiSearch(dto.getEntry_name());
            if (tdWikiSearch != null) {
                tdWikiSearchDao.updateTdWikiSearch(tdWikiSearch);
            } else {
                tdWikiSearch = new TdWikiSearch();
                tdWikiSearch.setSearch_param(dto.getEntry_name());
                tdWikiSearch.setSearch_size(1);
                tdWikiSearchDao.insertTdWikiSearch(tdWikiSearch);
            }


        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    百科详情
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     WikiListVo
     */
    public TdWikiDetailVo wikiDetail (TdWikiDetailDto dto, String token) {
        String logInfo = this.getClass().getName() + ":wikiDetail:";
        TdWikiDetailVo retInfo = new TdWikiDetailVo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdWikiDetail detail = tdWikiDao.findTdWikiDetail(dto.getWiki_id());
            if (detail != null) {
                List<TdWikiConList> list = tdWikiConDao.findTdWikiConList(detail.getWiki_id());
                if (list != null && list.size() > 0) {
                    detail.setCons(list);
                }
                List<TdWikiSearchList> searchList = tdWikiSearchDao.findTdWikiSearchList();
                if (searchList != null && searchList.size() > 0) {
                    detail.setSearchs(searchList);
                }
                if (redisTemplate.opsForValue().get(token) != null) {
                    User user = (User)redisTemplate.opsForValue().get(token);
                    TdWikiCollection collection = tdWikiCollectionDao.findTdWikiCollection(user.getUser_id(), detail.getWiki_id());
                    if (collection != null && collection.getState() == 0) {
                        detail.setIs_collection(0);
                    }
                    TdWikiPraise tdWikiPraise = tdWikiPraiseDao.findTdWikiPraise(user.getUser_id(), detail.getWiki_id());
                    if (tdWikiPraise != null) {
                        detail.setIs_praise(0);
                    }
                }
                retInfo.setDetail(detail);
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("百科不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    百科收藏
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo collection(TdWikiCollectionInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":collection:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdWikiDetail detail = tdWikiDao.findTdWikiDetail(dto.getWiki_id());
            if (detail != null) {

                TdWikiCollection collection = tdWikiCollectionDao.findTdWikiCollection(user.getUser_id(), detail.getWiki_id());
                if (collection != null) {
                    if (collection.getState() == 0) {
                        retInfo.setMark("1");
                        retInfo.setTip("已收藏");
                    } else {
                        collection.setState(0);
                        tdWikiCollectionDao.updateTdWikiCollectionState(collection);
                        retInfo.setMark("0");
                        retInfo.setTip("收藏成功");
                    }
                } else {
                    collection = new TdWikiCollection();
                    collection.setWiki_id(dto.getWiki_id());
                    collection.setUser_id(user.getUser_id());
                    tdWikiCollectionDao.insertTdWikiCollection(collection);
                    retInfo.setMark("0");
                    retInfo.setTip("收藏成功");
                }

            } else {
                retInfo.setMark("1");
                retInfo.setTip("百科不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    百科取消收藏
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo collectionCancel(TdWikiCollectionInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":collectionCancel:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdWikiDetail detail = tdWikiDao.findTdWikiDetail(dto.getWiki_id());
            if (detail != null) {
                TdWikiCollection collection = tdWikiCollectionDao.findTdWikiCollection(user.getUser_id(), detail.getWiki_id());
                if (collection != null) {
                    if (collection.getState() == 0) {
                        collection.setState(1);
                        tdWikiCollectionDao.updateTdWikiCollectionState(collection);
                        retInfo.setMark("0");
                        retInfo.setTip("取消收藏成功");

                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("未收藏");
                    }
                } else {

                    retInfo.setMark("1");
                    retInfo.setTip("未收藏");
                }

            } else {
                retInfo.setMark("1");
                retInfo.setTip("百科不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    百科点赞
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo praise(TdWikiPraiseInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":praise:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdWikiDetail detail = tdWikiDao.findTdWikiDetail(dto.getWiki_id());
            if (detail != null) {

                TdWikiPraise tdWikiPraise = tdWikiPraiseDao.findTdWikiPraise(user.getUser_id(), detail.getWiki_id());
                if (tdWikiPraise != null) {
                    if (tdWikiPraise.getState() == 0) {
                        retInfo.setMark("1");
                        retInfo.setTip("已点赞");
                    } else {
                        tdWikiPraise.setState(0);
                        tdWikiPraiseDao.updateTdWikiPraise(tdWikiPraise);
                        retInfo.setMark("0");
                        retInfo.setTip("点赞成功");
                    }
                } else {
                    tdWikiPraise = new TdWikiPraise();
                    tdWikiPraise.setWiki_id(dto.getWiki_id());
                    tdWikiPraise.setUser_id(user.getUser_id());
                    tdWikiPraiseDao.insertTdWikiPraise(tdWikiPraise);
                    retInfo.setMark("0");
                    retInfo.setTip("点赞成功");
                }

            } else {
                retInfo.setMark("1");
                retInfo.setTip("百科不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    百科取消点赞
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo praiseCancel(TdWikiPraiseInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":praiseCancel:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdWikiDetail detail = tdWikiDao.findTdWikiDetail(dto.getWiki_id());
            if (detail != null) {
                TdWikiPraise tdWikiCollection = tdWikiPraiseDao.findTdWikiPraise(user.getUser_id(), detail.getWiki_id());
                if (tdWikiCollection != null) {
                    if (tdWikiCollection.getState() == 0) {
                        tdWikiCollection.setState(1);
                        tdWikiPraiseDao.updateTdWikiPraise(tdWikiCollection);
                        retInfo.setMark("0");
                        retInfo.setTip("取消点赞成功");

                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("未点赞");
                    }
                } else {

                    retInfo.setMark("1");
                    retInfo.setTip("未点赞");
                }

            } else {
                retInfo.setMark("1");
                retInfo.setTip("百科不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    添加百科
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public ResultVo insertWiki(TdWikiInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":insertWiki:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            if (dto.getCons().size() > 0) {
                TsJzDict dict = tsJzDictDao.findDictById(dto.getEntry_type());
                if (dict != null && dict.getCode_type().equals("entry_type")) {
                    User user = (User)redisTemplate.opsForValue().get(token);
                    TdWiki tdWiki = new TdWiki();
                    tdWiki.setChinese_name(dto.getChinese_name());
                    tdWiki.setEntry_name(dto.getEntry_name());
                    tdWiki.setEntry_intro(dto.getEntry_intro());
                    tdWiki.setEntry_type(dto.getEntry_type());
                    tdWiki.setImpl_time(dto.getImpl_time());
                    tdWiki.setRelease_unit(dto.getRelease_unit());
                    tdWiki.setRevision_time(dto.getRevision_time());
                    tdWiki.setUser_id(user.getUser_id());

                    tdWikiDao.insertTdWiki(tdWiki);
                    for (int i = 0, size = dto.getCons().size(); i < size; i ++) {
                        TdWikiCon tdWikiCon = new TdWikiCon();
                        tdWikiCon.setCon_name(dto.getCons().get(i).getCon_name());
                        tdWikiCon.setWiki_con(dto.getCons().get(i).getWiki_con());
                        tdWikiCon.setWiki_id(tdWiki.getWiki_id());
                        tdWikiConDao.insertTdWikiCon(tdWikiCon);

                    }
                    retInfo.setMark("0");
                    retInfo.setTip("成功");
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("词条类别不对");
                }

            } else {
                retInfo.setMark("1");
                retInfo.setTip("最少一个词条内容");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    编辑百科
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public ResultVo updateWiki(TdWikiUpdateDto dto, String token) {
        String logInfo = this.getClass().getName() + ":updateWiki:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            if (dto.getCons().size() > 0) {
                TsJzDict dict = tsJzDictDao.findDictById(dto.getEntry_type());
                if (dict != null && dict.getCode_type().equals("entry_type")) {
                    TdWiki tdWiki = tdWikiDao.findTdWiki(dto.getWiki_id());
                    User user = (User)redisTemplate.opsForValue().get(token);
                    if (tdWiki != null && user.getUser_id() == tdWiki.getUser_id()) {


                        tdWiki.setChinese_name(dto.getChinese_name());
                        tdWiki.setEntry_name(dto.getEntry_name());
                        tdWiki.setEntry_intro(dto.getEntry_intro());
                        tdWiki.setEntry_type(dto.getEntry_type());
                        tdWiki.setImpl_time(dto.getImpl_time());
                        tdWiki.setRelease_unit(dto.getRelease_unit());
                        tdWiki.setRevision_time(dto.getRevision_time());
                        tdWiki.setUser_id(user.getUser_id());


                        for (int i = 0, size = dto.getCons().size(); i < size; i ++) {
                            TdWikiCon tdWikiCon = tdWikiConDao.findTdWikiCon(dto.getCons().get(i).getWiki_con_id());
                            if (tdWikiCon != null && tdWikiCon.getWiki_id() == tdWiki.getWiki_id()) {
                                tdWikiCon.setCon_name(dto.getCons().get(i).getCon_name());
                                tdWikiCon.setWiki_con(dto.getCons().get(i).getWiki_con());

                                tdWikiConDao.updateTdWikiCon(tdWikiCon);
                            } else {
                                retInfo.setMark("1");
                                retInfo.setTip(dto.getCons().get(i).getCon_name() + "  不存在");
                                return retInfo;
                            }


                        }
                        tdWikiDao.updateTdWiki(tdWiki);
                        retInfo.setMark("0");
                        retInfo.setTip("成功");
                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("词条不存在");
                    }
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("词条类别不对");
                }

            } else {
                retInfo.setMark("1");
                retInfo.setTip("最少一个词条内容");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    删除百科
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public ResultVo deleteWiki(TdWikiDeleteDto dto, String token) {
        String logInfo = this.getClass().getName() + ":deleteWiki:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdWiki tdWiki = tdWikiDao.findTdWiki(dto.getWiki_id());
            User user = (User)redisTemplate.opsForValue().get(token);
            if (tdWiki != null && user.getUser_id() == tdWiki.getUser_id()) {
                tdWiki.setState(4);
                tdWikiDao.updateTdWikiState(tdWiki);
                retInfo.setMark("0");
                retInfo.setTip("成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("词条不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    删除百科模块
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public ResultVo deleteWikiCon(TdWikiConDeleteDto dto, String token) {
        String logInfo = this.getClass().getName() + ":deleteWikiCon:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdWikiCon tdWikiCon = tdWikiConDao.findTdWikiCon(dto.getWiki_con_id());
            if (tdWikiCon != null) {
                TdWiki tdWiki = tdWikiDao.findTdWiki(tdWikiCon.getWiki_id());
                User user = (User)redisTemplate.opsForValue().get(token);
                if (tdWiki != null && user.getUser_id() == tdWiki.getUser_id()) {
                    tdWikiConDao.updateTdWikiConState(tdWikiCon);
                    tdWiki.setState(1);
                    tdWikiDao.updateTdWikiState(tdWiki);
                    retInfo.setMark("0");
                    retInfo.setTip("成功");
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("词条不存在");
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("模块不存在");
            }


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    我的百科列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public TdWikiUserListVo wikiUserList(Page dto, String token) {
        String logInfo =  ":wikiUserList:";
        logger.info("======" + logInfo + "begin======");
        TdWikiUserListVo vo = new TdWikiUserListVo();
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            String orderBy = "wiki_id asc";
            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
            List<TdWikiUserList> lists = tdWikiDao.findTdWikiUserList(user.getUser_id());
            PageInfo<TdWikiUserList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {
                vo.setCurrent_page(dto.getCurrent_page());
                vo.setList(list.getList());
                vo.setPage_size(list.getPageSize());
                vo.setTotal_record(list.getTotal());
                vo.setPages(list.getPages());
                vo.setMark("0");
                vo.setTip("查询成功");
            } else {
                vo.setMark("1");
                vo.setTip("无记录");
            }



        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    我的百科详情
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     WikiListVo
     */
    public TdWikiUserDetailVo wikiUserDetail (TdWikiDetailDto dto, String token) {
        String logInfo = this.getClass().getName() + ":wikiUserDetail:";
        TdWikiUserDetailVo retInfo = new TdWikiUserDetailVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdWikiUserDetail detail = tdWikiDao.findTdWikiUserDetail(dto.getWiki_id(), user.getUser_id());
            if (detail != null ) {
                List<TdWikiConList> list = tdWikiConDao.findTdWikiConList(detail.getWiki_id());
                if (list != null && list.size() > 0) {
                    detail.setCons(list);
                }
                retInfo.setDetail(detail);
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("百科不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
